# build_simple.py
import os
import subprocess
import sys
import urllib.request

def download_model():
    """下载OpenCV模型文件"""
    model_url = "https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml"
    model_path = "haarcascade_frontalface_default.xml"
    
    if not os.path.exists(model_path):
        print("下载人脸检测模型...")
        urllib.request.urlretrieve(model_url, model_path)
        print("下载完成")
    else:
        print("模型已存在")

def build_with_pyinstaller():
    """使用PyInstaller打包"""
    cmd = [
        sys.executable, "-m", "PyInstaller",
        "face_detection_app2.py",
        "--name=FaceDetectionApp",
        "--onefile",
        "--windowed",
        "--clean",
        "--noconfirm",
        "--add-data", "haarcascade_frontalface_default.xml;.",
        "--exclude-module", "matplotlib",
        "--icon", "E:\\myproject\\aiface\\favicon2.ico",
        "--exclude-module", "scipy",
        "--exclude-module", "pandas",
        "--hidden-import", "PIL",
        "--hidden-import", "tkinter",
    ]

    print("开始打包...")
    subprocess.run(cmd)
    print("打包完成！输出文件在 dist/ 目录中")

if __name__ == "__main__":
    download_model()
    build_with_pyinstaller()